<?php

namespace frontend\modules\home\controllers;

use Yii;
use yii\web\Controller;
use common\helpers\ResultDataHelper;
use backend\modules\home\models\Consult;
use common\components\MobileDetect;
/**
 * Default controller for the `Home` module
 */
class DefaultController extends Controller
{
    /**
     * Renders the index view for the module
     * @return string
     */

    public $layout = 'main';
    public $enableCsrfValidation = false;

    public function actionIndex()
    {
        $detect = new MobileDetect();
        if($detect->isMobile()||$detect->isTablet()){
            return $this->redirect('/touch/default/index');
        }
//        $this->layout = false;

        return $this->render('index');
    }

    public function actionPrice(){
        return $this->render('price');
    }

    public function actionConsult(){
    	if (Yii::$app->request->isPost) {
            $model = new Consult();
            $postdata = Yii::$app->request->post(NULL);
            $time = time();

            if(!isset($postdata['phone'])){
                return ResultDataHelper::result(201,'参数错误',[]);
            }
            // select
            $count = $model::find()->where(['phone' => $postdata['phone']])
                    ->andWhere((['between','create_at',$time - 30 * 60, $time]))
                    ->count();
            if($count >= 2){
                return ResultDataHelper::result(201,'半个小时内提交不能超过2次',[]);
            }  
            // create
            $model->usename = $postdata['usename'];
            $model->phone = $postdata['phone'];
            $model->question = $postdata['question'];
            if ($model->save()) {
                return ResultDataHelper::result(200,'success',[]);
            }else{
                return ResultDataHelper::result(201,'参数错误',[]);
            }
        }else{
            return ResultDataHelper::result(201,'非法请求',[]);
        }
    }
}
